<?

require_once(dirname(__FILE__) . '/autoloader.php');

class bf {

	public static $application;

	public static $request;
	public static $html;
	public static $url;
	public static $http;
	public static $inet;

	protected static $libsdir = false;

	public static function init() {
		bf::$request = new bf_request();
		bf::$html = new bf_html();
		bf::$url = new bf_url();
		bf::$http = (function_exists('curl_init') && function_exists('curl_setopt_array')
			? new bf_http_curl()
			: new bf_http_sock()
		);
		$inet_impl = 'inet.ipaddress.com'; // TODO must be configurable
		bf::$inet = ($inet_impl == 'inet.externalip.net'
			? new bf_inet_externalip_net()
			: ($inet_impl == 'inet.dyndns.org'
				? new bf_inet_dyndns_org()
				: ($inet_impl == 'inet.ipaddress.com'
					? new bf_inet_ipaddress_com()
					: null
				)
			)
		);
	}

	public static function url($route, $application_context = true, $schema = false) {
		if ($application_context && bf::$application) {
			return bf::$url->get(
				bf_path::concat(bf::$application->location, $route),
				$schema
			);
		} else {
			return bf::$url->get(
				preg_replace('/^\/+/', '', $route),
                		$schema
			);
		}
	}

	public static function date($format, $date) {
		$date = new bf_date($date);
		return $date->get($format);
	}

	public static function view($file, $params = array(), $to_string = false) {
		$result = true;
		if (bf::$application)
			$file = array('location' => bf::$application->views, 'file' => $file);
		$view = new bf_view($file);
		foreach ($params as $name => $value)
			$view->$name = $value;
		if ($to_string) {
			ob_start();
			$view->render();
			$result = ob_get_contents();
			ob_end_clean();
		} else {
			$view->render();
		}
		return $result;
	}
}

// init

bf::init();

?>